package validators;

import java.util.List;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import dtos.MeasureunitDTO;
import utils.MeasureunitDAORemote;

@FacesValidator("validators.MeasureDuplicateValidator")
public class MeasureDuplicateValidator implements Validator {

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());
	
	@EJB
	MeasureunitDAORemote mudr;

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		List<MeasureunitDTO> units = mudr
				.getAllMeasureunitsByName(((String) value).trim());
		if (!units.isEmpty()) {
			FacesMessage message = new FacesMessage(
					bundle.getString("Validators_MeasureDuplicateValidator_Error"));
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
	}

}
